package io;

import static net.mindview.util.Print.print;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class Blip3MY implements Externalizable {
	private int i;
	private String s; // No initialization

	public Blip3MY() {
		print("Blip3MY Constructor");
		// s, i not initialized
	}

	public Blip3MY(String x, int a) {
		print("Blip3MY(String x, int a)");
		s = x;
		i = a;
		// s & i initialized only in non-default constructor.
	}

	public String toString() {
		return s + i;
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		print("Blip3MY.writeExternal");
		// You must do this:
		out.writeObject(s);
		out.writeInt(i);
	}

	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		print("Blip3MY.readExternal");
		// You must do this:
		s = (String) in.readObject();
		i = in.readInt();
	}

	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		print("Constructing objects:");
		Blip3MY b3 = new Blip3MY("A String ", 47);
		print(b3);
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
				"Blip3MY.out"));
		print("Saving object:");
		o.writeObject(b3);
		o.close();
		// Now get it back:
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				"Blip3MY.out"));
		print("Recovering b3:");
		b3 = (Blip3MY) in.readObject();
		print(b3);
		in.close();
	}
}
